package com.gable.exhibition.thread;

import com.gable.common.util.StringUtil;
import com.gable.exhibition.dao.UserDao;
import com.gable.exhibition.util.HttpHelper;
import com.gable.exhibition.view.UserView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 发货时指派其他服务商（厂商）
 * Created by mj on 2018/3/28.
 */
public class EstimateSendDispatchSmsThread implements Runnable{
    Logger logger = LoggerFactory.getLogger(this.getClass());

    private String hospitalName;        //医院名称

    private String serviceProviderName;   //乙方名称

    private String contractNo;          //合同编号

    private Long serviceType;   //服务商类型    1102 厂商 1103 维修商

    private Long serviceId;         //服务商ID

    private UserDao userDao;

    private String url;     //发送短信请求url

    public EstimateSendDispatchSmsThread(String hospitalName,String serviceProviderName,String contractNo,
                                         Long serviceType,Long serviceId,UserDao userDao,String url){
        this.hospitalName = hospitalName;
        this.serviceProviderName = serviceProviderName;
        this.contractNo = contractNo;
        this.serviceType = serviceType;
        this.serviceId = serviceId;
        this.userDao = userDao;
        this.url = url;
    }
    @Override
    public void run() {
        try {
            UserView userView = new UserView();
            if(serviceType.intValue() == 1102){
                userView.setManufacturerId(serviceId);
            }else if(serviceType.intValue() == 1103){
                userView.setMaintainerId(serviceId);
            }
            List<String> usersMobile = userDao.getUsersMobile(userView);
            StringBuilder sb = new StringBuilder();
            if(usersMobile != null && usersMobile.size() > 0){
                for(String phone : usersMobile){
                    if(StringUtil.isEmpty(phone))
                        continue;
                    sb.append(phone).append(",");
                }
                String mobile = sb.substring(0, sb.length() - 1);
                String message = hospitalName+"%&"+serviceProviderName+"%&"+contractNo;
                Map<String,String> map = new HashMap<String,String>();
                map.put("mobile",mobile);
                map.put("message",message);
                String jsonResult = HttpHelper.postRequestResponseBodyAsString(url, map);
                logger.info("发货指派服务商，推送给服务商的短信合同编号："+contractNo+",短信发送结果:"+jsonResult);
            }
        } catch (Exception e) {
            logger.error("发货指派服务商，推送给服务商的短信合同编号："+contractNo+",短信发送失败:"+e);
        }
    }
}
